<?php
namespace app\admin\controller;

use think\Controller;
//权限管理
class Menu extends Common
{
	//权限列表
    public function menu_list()
    {
		$where=array('a.m_id'=>array('gt',0));
		$name=Request()->param('name');
		//权限名称
		$status=Request()->param('status');
		//权限状态太
		if($name!=''){
			$where['a.m_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.m_status']=1;
			}else{
				$where['a.m_status']=array('neq',1);
			}
			
		}
		$menu1=db('menu a')
			->field("a.*,b.m_name as m_p_name")
			->join('fr_menu b','a.m_p_id = b.m_id','left')
			->order('a.m_status,a.m_sort desc,a.m_id')
			->where($where)
			->where(array('a.m_type'=>1))
			->select();
		//一级权限
		$menu2=db('menu a')
			->field("a.*,b.m_name as m_p_name")
			->join('fr_menu b','a.m_p_id = b.m_id','left')
			->order('a.m_status,a.m_sort desc,a.m_id')
			->where($where)
			->where(array('a.m_type'=>2))
			->select();	
		//二级权限
		foreach($menu1 as $k=>$v){
			$menu[]=$v;
			foreach($menu2 as $kk=>$vv){
				if($vv['m_p_id']==$v['m_id']){
					$menu[]=$vv;
				}
			}
		}
		$this->view->assign('menu',$menu);
		$this->assign('status',$status);
		$this->assign('name',$name);
        return $this->view->fetch('index');
		//加载index页面
    }
	//添加菜单
	public function add_menu(){		
		if(request()->isGet()){
			//get，加载页面
			$menu=db('menu')->where(array('m_status'=>1,'m_type'=>1))->order("m_sort desc")->select();
			//一级可用权限
			$this->view->assign('menu',$menu);
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();			
			$data['m_name']=trim($data['m_name']);
			//权限么
			$data['m_url']=trim($data['m_url']);
			//链接地址
			if($data['m_name']==''){
				return return_json(array('code'=>0,'msg'=>'菜单名称不能为空'),'添加菜单');
			}
			if($data['m_p_id']!=0 && $data['m_url']==''){
				return return_json(array('code'=>0,'msg'=>'二级菜单必须有链接地址'),'添加菜单');
			}			
			if($data['m_sort']==''){
				$data['m_sort']=0;
			}
			if($data['m_p_id']==0){
				$data['m_type']=1;
			}else{
				$data['m_type']=2;
			}
			$data['m_create_time']=time();
			$menu=db('menu')->where(array('m_url'=>$data['m_url'],'m_name'=>$data['m_name'],'m_p_id'=>$data['m_p_id']))->find();
			//有无相同菜单
			if(!empty($menu)){
				return return_json(array('code'=>0,'msg'=>'该菜单已存在'),'添加菜单');
			}else{
				$res=db('menu')->insertGetId($data);
				//添加权限
				if($res>0){
					return return_json(array('code'=>1,'msg'=>'菜单添加成功'),'添加菜单');
				}else{
					return return_json(array('code'=>0,'msg'=>'菜单添加失败'),'添加菜单');
				}
			}
		}
    }
	//修改菜单
	public function remenu(){		
		if(request()->isGet()){
			$id=request()->get('id');
			//权限id
			$t_menu=db('menu')->where(array('m_id'=>$id))->find();
			//本权限
			$menu=db('menu')->where(array('m_status'=>1,'m_type'=>1))->order("m_sort desc")->select();
			//以及权限
			$this->view->assign('menu',$menu);
			$this->view->assign('t_menu',$t_menu);
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();
			$id=$data['id'];
			unset($data['id']);
			$data['m_name']=trim($data['m_name']);
			$data['m_url']=trim($data['m_url']);
			if($data['m_name']==''){
				return return_json(array('code'=>0,'msg'=>'菜单名称不能为空'),'修改菜单');
			}
			if($data['m_p_id']!=0 && $data['m_url']==''){
				return return_json(array('code'=>0,'msg'=>'二级菜单必须有链接地址'),'修改菜单');
			}			
			if($data['m_sort']==''){
				$data['m_sort']=0;
			}
			if($data['m_p_id']==0){
				$data['m_type']=1;
			}else{
				$data['m_type']=2;
			}
			$menu=db('menu')->where(array('m_url'=>$data['m_url'],'m_name'=>$data['m_name'],'m_p_id'=>$data['m_p_id'],'m_id'=>array('neq',$id)))->find();
			if(!empty($menu)){
				return return_json(array('code'=>0,'msg'=>'有相同的菜单'),'修改菜单');
			}else{
				$res=db('menu')->where(array('m_id'=>$id))->update($data);
				//修改权限
				if($res!==false){
					return return_json(array('code'=>1,'msg'=>'菜单编辑成功'),'修改菜单');
				}else{
					return return_json(array('code'=>0,'msg'=>'菜单编辑失败'),'修改菜单');
				}
			}
		}
    }
	//禁用菜单
	function del_menu(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('menu')
				->where(array('m_id'=>$id))
				->update(array('m_status'=>2));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用菜单');
			}else{
				return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用菜单');
			}
		}
	}
	//启用菜单
	function use_menu(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('menu')
				->where(array('m_id'=>$id))
				->update(array('m_status'=>1));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'启用成功'),'启用菜单');
			}else{
				return return_json(array('code'=>0,'msg'=>'启用失败'),'启用菜单');
			}
		}
	}
	//角色管理
	function role_list(){
		$where['a.rid']=array('neq',1);
		$name=Request()->param('name');
		//角色名称
		$status=Request()->param('status');
		//角色状态
		$page=Request()->param('page');
		$size=Request()->param('size');
		//分页
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where['a.name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.status']=1;
			}else{
				$where['a.status']=array('neq',1);
			}
		}
		$role=db('role a')
			->field("a.*,count(b.sid) as count")
			->join('fr_staff b','a.rid=b.role_id','left')
			->where($where)
			->group("a.rid")
			->limit("$now,$size")
			->order("a.order desc,a.rid desc")
			->select();
		//角色信息
		$count=db('role a')
			->where($where)
			->count();
		$page_str=page($page,$size,$count,count($role));//分页
		$this->assign('name',$name);
		$this->assign('status',$status);
		$this->assign('role',$role);
		$this->assign('count',$count);
		$this->assign('page',$page);
		$this->assign('size',$size);
		$this->assign('page_str',$page_str);
		return $this->fetch();
	}
	//添加角色
	function add_role(){
		if(Request()->isAjax()){
			$permissions=$this->check_permissions();
			if($permissions['code']!=200){
				return $permissions;die;
			}
			$data=Request()->post();
			$name=$data['name'];//角色名
			$order=$data['order'];
			//排序
			if($name==''){
				return return_json(array('code'=>0,'msg'=>'角色名称不能为空'),'添加角色');die;
			}
			if(isset($data['status'])){
				$status=1;
			}else{
				$status=0;
			}
			//状态
			if(!empty($data['menus'])){
				$menus=implode(",",$data['menus']);
			}else{
				$menus='';
			}
			//权限
			$role=db('role')
				->where(array('name'=>$name))
				->find();
			if(!empty($role)){
				return return_json(array('code'=>0,'msg'=>'已有该角色'),'添加角色');die;
			}
			$res=db('role')
				->insert(array('name'=>$name,'status'=>$status,'order'=>$order,'menus'=>$menus,'add_time'=>time(),'add_user_id'=>session('uid'),'add_user_name'=>session('user.real_name')));
			if($res){
				return return_json(array('code'=>1,'msg'=>'角色添加成功！'),'添加角色');die;
			}else{
				return return_json(array('code'=>1,'msg'=>'角色添加失败！'),'添加角色');die;
			}
		}else{
			$where=array('a.m_status'=>1);
			$menu1=db('menu a')
			->field("a.*,b.m_name as p_name")
			->join('fr_menu b','b.m_id=a.m_p_id','left')
			->where($where)
			->where(array('a.m_type'=>1))
			->order("a.m_status desc,a.m_sort desc,a.m_id desc")
			->select();
			// 一级权限
			$menu2=db('menu a')
				->field("a.*,b.m_name as p_name")
				->join('fr_menu b','b.m_id=a.m_p_id','left')
				->where($where)
				->where(array('a.m_type'=>2))
				->order("a.m_status desc,a.m_sort desc,a.m_id desc")
				->select();
			// 二级权限
			/*$menu3=db('menu a')
				->field("a.*,b.m_name as p_name")
				->join('fr_menu b','b.m_id=a.m_p_id','left')
				->where($where)
				->where(array('a.type'=>3))
				->order("a.m_status desc,a.m_sort desc,a.m_id desc")
				->select();*/
			//三级功能权限使用
			$this->assign('menu1',$menu1);
			$this->assign('menu2',$menu2);
			// $this->assign('menu3',$menu3);
			return $this->fetch();
		}
	}
	//修改角色
	function rerole(){
		if(Request()->isAjax()){
			$permissions=$this->check_permissions();
			if($permissions['code']!=200){
				return $permissions;die;
			}
			$data=Request()->post();
			$id=$data['id'];
			unset($data['id']);
			$name=$data['name'];
			$order=$data['order'];
			if($name==''){
				return return_json(array('code'=>0,'msg'=>'角色名称不能为空'),'角色修改');die;
			}
			if(isset($data['status'])){
				$status=1;
			}else{
				$status=0;
			}
			if(!empty($data['menus'])){
				$menus=implode(",",$data['menus']);
			}else{
				$menus='';
			}
			$role=db('role')
				->where(array('name'=>$name,'rid'=>array('neq',$id)))
				->find();
			if(!empty($role)){
				return return_json(array('code'=>0,'msg'=>'已有该角色'),'角色修改');die;
			}
			$res=db('role')
				->where(array('rid'=>$id))
				->update(array('name'=>$name,'status'=>$status,'order'=>$order,'menus'=>$menus));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'角色修改成功！'),'角色修改');die;
			}else{
				return return_json(array('code'=>1,'msg'=>'角色修改失败！'),'角色修改');die;
			}
		}else{
			$id=Request()->get('id');
			$role=db('role')
				->where(array('rid'=>$id))
				->find();
			$menus=explode(',',$role['menus']);
			$where=array('a.m_status'=>1);
			$menu1=db('menu a')
			->field("a.*,b.m_name as p_name")
			->join('fr_menu b','b.m_id=a.m_p_id','left')
			->where($where)
			->where(array('a.m_type'=>1))
			->order("a.m_status desc,a.m_sort desc,a.m_id desc")
			->select();
		
			$menu2=db('menu a')
				->field("a.*,b.m_name as p_name")
				->join('fr_menu b','b.m_id=a.m_p_id','left')
				->where($where)
				->where(array('a.m_type'=>2))
				->order("a.m_status desc,a.m_sort desc,a.m_id desc")
				->select();
		
			/*$menu3=db('menu a')
				->field("a.*,b.m_name as p_name")
				->join('fr_menu b','b.m_id=a.m_p_id','left')
				->where($where)
				->where(array('a.type'=>3))
				->order("a.m_status desc,a.m_sort desc,a.m_id desc")
				->select();*/
			//三级功能权限使用
			$this->assign('role',$role);
			$this->assign('menus',$menus);
			$this->assign('menu1',$menu1);
			$this->assign('menu2',$menu2);
			// $this->assign('menu3',$menu3);
			return $this->fetch();
		}
	}
	//禁用角色
	function role_del(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('role')
				->where(array('rid'=>$id))
				->update(array('status'=>2));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用角色');
			}else{
				return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用角色');
			}
		}
	}
	//启用角色
	function role_use(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('role')
				->where(array('rid'=>$id))
				->update(array('status'=>1));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'禁用成功'),'启用角色');
			}else{
				return return_json(array('code'=>0,'msg'=>'禁用失败'),'启用角色');
			}
		}
	}
}
